package com.cts.tmall.biz.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cts.tmall.dal.entity.TmallEva;
import com.cts.tmall.dal.mapper.TmallEvaMapper;
import com.cts.tmall.dal.mapper.TmallOrdMapper;
import com.cts.tmall.dal.mapper.TmallPrdMapper;
import com.google.common.collect.ImmutableMap;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 评价管理
 *
 * @author leibing.song
 */
@Service
public class EvaService {

    @Autowired
    private TmallEvaMapper evaMapper;
    @Autowired
    private TmallPrdMapper prdMapper;
    @Autowired
    private TmallOrdMapper ordMapper;

    public Map<String, Object> queryEva(Map<String, String> params) throws ParseException {
        Long pageNumb = StringUtils.isBlank(params.getOrDefault("pageNumb", null)) ? 1L : Long.parseLong(params.get("pageNumb"));
        Long pageSize = StringUtils.isBlank(params.getOrDefault("pageSize", null)) ? 10L : Long.parseLong(params.get("pageSize"));

        Page<TmallEva> page = new Page<>(pageNumb, pageSize);

        LambdaQueryWrapper<TmallEva> wrapper = Wrappers.<TmallEva>lambdaQuery()
                .eq(StringUtils.isNotBlank(params.get("prdNo")), TmallEva::getPrdNo, params.get("prdNo"))
                .eq(StringUtils.isNotBlank(params.get("prdName")), TmallEva::getPrdName, params.get("prdName"))
                .eq(StringUtils.isNotBlank(params.get("evaLevel")), TmallEva::getEvaLevel, params.get("evaLevel"))
                .eq(StringUtils.isNotBlank(params.get("customNo")), TmallEva::getCustomNo, params.get("customNo"))
                .eq(StringUtils.isNotBlank(params.get("isReturn")), TmallEva::getIsReturn, params.get("isReturn"))
                .eq(StringUtils.isNotBlank(params.get("ordNo")), TmallEva::getOrdNo, params.get("ordNo"))
                .eq(StringUtils.isNotBlank(params.get("isHaveImg")), TmallEva::getIsHaveImg, params.get("isHaveImg"))
                .eq(StringUtils.isNotBlank(params.get("isAddEva")), TmallEva::getIsAddEva, params.get("isAddEva"))
                .eq(StringUtils.isNotBlank(params.get("isChangeEva")), TmallEva::getIsChangeEva, params.get("isChangeEva"));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (StringUtils.isNotBlank(params.get("createTime1"))) {
            wrapper.ge(TmallEva::getCreateTime, sdf.parse(params.get("createTime1")));
        }
        if (StringUtils.isNotBlank(params.get("createTime2"))) {
            wrapper.le(TmallEva::getCreateTime, sdf.parse(params.get("createTime2")));
        }

        page = evaMapper.selectPage(page, wrapper);
        return ImmutableMap.<String, Object>builder()
                .put("pageNumb", page.getCurrent())
                .put("pageSize", page.getSize())
                .put("total", page.getTotal())
                .put("data", page.getRecords().stream().map(eva ->
                        ImmutableMap.<String, Object>builder()
                                .put("evaNo", eva.getEvaNo())
                                .put("prd", prdMapper.selectById(eva.getPrdNo()))
                                .put("evaLevel", eva.getEvaLevel())
                                .put("customNo", eva.getCustomNo())
                                .put("createTime", sdf.format(eva.getCreateTime()))
                                .put("isReturn", eva.getIsReturn())
                                .put("ord", ordMapper.selectById(eva.getOrdNo()))
                                .put("isHavaImg", eva.getIsHaveImg())
                                .put("isAddEva", eva.getIsAddEva())
                                .put("isChangeEva", eva.getIsChangeEva())
                                .build()
                ).collect(Collectors.toList()))
                .build();
    }
}
